//
//  ABManager.m
//  GroupManager
//
//  Created by gokei on 11/10/12.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "ABManager.h"


@implementation ABManager

@synthesize groups, addressBook;

static ABManager *_sharedManager;

#pragma mark -
#pragma mark Override

- (id)init {
	if (self = [super init]) {
		[self initAddressBookInfo];
	}
	return self;
}

- (void)dealloc {
	if (addressBook) {
		CFRelease(addressBook);
	}
	if (groups) {
		[groups release];
	}
	[super dealloc];
}

+ (ABManager*)sharedManager {
	if (!_sharedManager) {
		_sharedManager = [[self alloc] init];
	}
	return _sharedManager;
}

#pragma mark -
#pragma mark AddressBook Management

- (void)initAddressBookInfo {
	if(!(addressBook = ABAddressBookCreate()))return;
	if (!groups) {
		groups = [[NSMutableArray alloc] initWithCapacity:4];
	}
	[self readAllGroups];
}

- (void)reloadAddressBook {
	if (addressBook) {
		CFRelease(addressBook);
		addressBook = nil;
		addressBook = ABAddressBookCreate();
	}
	[self readAllGroups];
}

- (void)saveAddressBook {
	ABAddressBookSave(addressBook, nil);
}

#pragma mark -
#pragma mark Group Management

- (void)readAllGroups {
	CFArrayRef arr = ABAddressBookCopyArrayOfAllGroups(addressBook);
	if (groups) {
		[groups removeAllObjects];
	}
	for (int i = 0; i < CFArrayGetCount(arr); i ++) {
		ABRecordRef record = CFArrayGetValueAtIndex(arr, i);
		GMGroup *group = [GMGroup groupWithGroup:record];
		[self readAllPersonsOfGroup:group];
		int index = 0;
		for (; index < [groups count]; index ++) {
			GMGroup *g = [groups objectAtIndex:index];
			if ([[group getName] localizedCaseInsensitiveCompare:[g getName]] == NSOrderedAscending) {
				[groups insertObject:group atIndex:index];
				break;
			}
		}
		if (index == [groups count]) {
			[groups addObject:group];
		}
	}
	CFRelease(arr);
}

- (int)getGroupCount {
	return [groups count];
}

- (NSString*)getGroupNameAtIndex:(int)index {
	return [[groups objectAtIndex:index] getName];
}

- (void)updateGroupNameAtIndex:(int)index name:(NSString*)name {
	[[groups objectAtIndex:index] setName:name];
	ABAddressBookSave(addressBook, nil);
}

- (void)createGroup:(NSString*)gpName {
	if (addressBook && [gpName length] > 0) {
		ABRecordRef gp = ABGroupCreate();
		ABRecordSetValue(gp, kABGroupNameProperty, gpName, nil);
		ABAddressBookAddRecord(addressBook, gp, nil);
		ABAddressBookSave(addressBook, nil);
		CFRelease(gp);
		[self readAllGroups];
	}
}

- (void)deleteGroupAtIndex:(int)index {
	if (addressBook && index < [groups count]) {
		ABAddressBookRemoveRecord(addressBook, [[groups objectAtIndex:index] group], nil);
		ABAddressBookSave(addressBook, nil);
		[groups removeObjectAtIndex:index];
	}
}

#pragma mark -
#pragma mark Person Management

- (void)readAllPersonsOfGroup:(GMGroup*)group {
	[group readAllPersonsOfGroup];
	return;
	CFArrayRef persons = ABGroupCopyArrayOfAllMembersWithSortOrdering([group group], kABPersonSortByLastName);
	if (persons) {
		group.personArray = CFArrayCreateMutableCopy(kCFAllocatorDefault, CFArrayGetCount(persons), persons);
		CFRelease(persons);
	} else {
		group.personArray = CFArrayCreateMutable(kCFAllocatorDefault, 0, nil);
	}
}

@end
